package com.fangshicai.interceptor;

import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.omg.PortableInterceptor.Interceptor;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 1.实现HandlerInterceptor
 * 2.重写三个方法
 * @ClassName loginInterceptor
 * @Description TODO
 * @Author fangshicai
 * @Date 2021/7/17 10:51
 */
@Slf4j
public class loginInterceptor implements HandlerInterceptor {
    //执行方法controller层前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
//        log.info("拦截路径：{}",requestURI);
        HttpSession session = request.getSession();
        Object loginUser = session.getAttribute("loginUser");
        if(loginUser!=null){

            return true;
        }
        request.setAttribute("msg","请先登陆");
        request.getRequestDispatcher("/").forward(request,response);

        return false;
    }
    //执行方法controller跳转前执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    //执行方法后
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
